<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=   , initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function myAxios(config){
            return new Promise((resolve, reject) => {

                const {url, method = 'get'} = config
                // 原生ajax 五步走
                // 1. 实例化xhr
                const xhr = new XMLHttpRequest()
                // 2. 设置方法和接口
                xhr.open(method, url)
                // 3. 请求头
                // xhr.setRequestHeader(key, val)
                // 4. 注册回调函数
                xhr.addEventListener('load', () => {
                    if(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304){
                        console.log()
                        resolve(JSON.parse(xhr.response))
                    } else {
                        reject('请求失败')
                    }
                })
                // 5. 发送请求
                xhr.send()
            })
        }


        const api = 'http://ajax-api.itheima.net/api/news'

        myAxios({
            url:api,
            // method:'get'
        }).then(res => {
            console.log(res)
        })
    </script>
</body>
</html>